package com.jungle.bfi.enums;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

/**
 * 记账方向枚举类
 */
public enum BookingDirection {
    /*借方*/
    DEBIT(1),
    /*贷方*/
    CREDIT(2);

    private final int code;

    BookingDirection(int code) {
        this.code = code;
    }

    public static BookingDirection codeOf(int dbData) {
        for (BookingDirection bookingDirection : values()) {
            if (bookingDirection.code == dbData) {
                return bookingDirection;
            }
        }
        return null;
    }

    public int code() {
        return code;
    }

    @jakarta.persistence.Converter(autoApply = true)
    public static class Converter implements AttributeConverter<BookingDirection, Integer> {
        @Override
        public Integer convertToDatabaseColumn(BookingDirection attribute) {
            return attribute.code();
        }

        @Override
        public BookingDirection convertToEntityAttribute(Integer dbData) {
            return BookingDirection.codeOf(dbData);
        }
    }
}
